﻿using System;
using System.Collections.Generic;

namespace Grinder.Settings
{
    public sealed class Section
    {
        private readonly IDictionary<string, string> _values = new Dictionary<string, string>();

        public string Name { get; set; }

        public Section(string name)
        {
            if (name == null) throw new ArgumentNullException("name");

            Name = name;
        }

        public void AddValue(string key, string value)
        {
            if (key == null) throw new ArgumentNullException("key");
            if (value == null) throw new ArgumentNullException("value");

            if (_values.ContainsKey(key))
                _values[key] = value;
            else
                _values.Add(key, value);
        }

        public string GetValue(string key)
        {
            if (key == null) throw new ArgumentNullException("key");

            string value;

            try
            {
                value = _values[key];
            }
            catch (KeyNotFoundException)
            {
                throw new KeyNotFoundException("Key not found: " + Name + "/" + key);
            }

            return value;
        }

        public IDictionary<string, string> Values { get { return _values; } }

        public void RemoveValue(string key)
        {
            if (key == null) throw new ArgumentNullException("key");

            _values.Remove(key);
        }
    }
}
